'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-01-14 10:16:16
'''
'''
The new meta info for the redis features, which redis to get the features.
'''
import pymysql
import json
final = []
conn = pymysql.connect(
    host = "172.19.8.66",
    port = 3306,
    user = "algorithm",
    password = "S4!4Eywru@aH",
    # database = "rcd_phoenix_admin",
    database = "rcd_kraken",
    charset='utf8'
    )

cursor = conn.cursor()

# sql = "show tables"
# cursor.execute(sql)
# results = cursor.fetchall()
# print(results)


'''
@note: get the code dimension and value_type from feature_meta.
'''
req_fea = ["code", "dimension", "value_type"]
req_ind = []
sql = "desc feature_meta"
cursor.execute(sql)

results = cursor.fetchall()
print(results)
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1
# print(req_ind)
# print(index)
final.append(index)
sql = "select * from feature_meta"
cursor.execute(sql)
results = list(cursor.fetchall())
# print(results[0])

for result in results:
    # print(result)
    final.append(list(result))

# print(final[0:3])

feature_meta = [[a[i] for i in req_ind] for a in final]
# for l in feature_meta:
#     print(l)
j_feature_store = {}
for l in feature_meta[1:]:
    j_feature_store[l[0]] = {"dimension":l[1], "value_type":l[2]}
# print(j_feature_store)




'''
@note: get the feature_code and ns_id from feature_namespace_mapping_config.
'''
final = []
req_fea = ["feature_code", "ns_id"]
req_ind = []
sql = "desc feature_namespace_mapping_config"
cursor.execute(sql)

results = cursor.fetchall()
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1
# print(req_ind)
# print(index)
final.append(index)
sql = "select * from feature_namespace_mapping_config"
cursor.execute(sql)
results = list(cursor.fetchall())


for result in results:
    # print(result)
    final.append(list(result))

# print(final[0:3])

feature_namespace_mapping_config = [[a[i] for i in req_ind] for a in final]
j_code_ns={}
for l in feature_namespace_mapping_config[1:]:
    # print(l)
    if l[0] not in j_code_ns.keys():
        j_code_ns[l[0]] = [l[1]]
    else:
        j_code_ns[l[0]].append(l[1])

# print(j_code_ns)


'''
@note: get the ns, storeage_id from feature_namespace_config.
'''
final = []
req_fea = ["id", "name", "storage_id"]
req_ind = []
sql = "desc feature_namespace_config"
cursor.execute(sql)

results = cursor.fetchall()
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1
# print(req_ind)
# print(index)
final.append(index)
sql = "select * from feature_namespace_config"
cursor.execute(sql)
results = list(cursor.fetchall())


for result in results:
    # print(result)
    final.append(list(result))

# print(final[0:3])

feature_namespace_mapping_config = [[a[i] for i in req_ind] for a in final]
j_nsid_ns_storeid={}
for l in feature_namespace_mapping_config[1:]:
    # print(l)
    j_nsid_ns_storeid[l[0]] = {"ns":l[1], "feature_storage_id":l[2]}
# print(j_nsid_ns_storeid)


'''
@note: get id, type and address from feature_storage_config
'''
final = []
req_fea = ["id", "type", "address"]
req_ind = []
sql = "desc feature_storage_config"
cursor.execute(sql)

results = cursor.fetchall()
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1
# print(req_ind)
# print(index)
final.append(index)
sql = "select * from feature_storage_config"
cursor.execute(sql)
results = list(cursor.fetchall())


for result in results:
    # print(result)
    final.append(list(result))

# print(final[0:3])

feature_storage_config = [[a[i] for i in req_ind] for a in final]
# print(feature_storage_config)
j_storageid_address={}
for l in feature_storage_config[1:]:
    # print(l)
    if l[1] == 1:
        j_storageid_address[l[0]] = l[2]
# print(j_storageid_address)

'''
@note: the redis_map generation.
'''
redis_map_store = json.dumps(j_storageid_address)
redis_map_store = redis_map_store.replace(', "', ',\n"')

# print(redis_map_store)
f_res = open("./redis_map", "w")
f_res.write(redis_map_store)
f_res.close()


'''
@note: deal with the j_feature_store by the other two map j_code_ns and j_nsid_ns_storeid.
'''
del_code = set()
redis_id = j_storageid_address.keys()
for k, v in j_feature_store.items():
    if k in j_code_ns.keys():
        for ns_ind in j_code_ns[k]:
            tmp_ns_si = j_nsid_ns_storeid[ns_ind]
            if tmp_ns_si["feature_storage_id"] not in redis_id:
                continue
            v.update(j_nsid_ns_storeid[ns_ind])
    else :
        del_code.add(k)
for k, v in j_feature_store.items():
    if "feature_storage_id" not in v.keys():
        del_code.add(k)
for k in del_code:
    del j_feature_store[k]
# print(j_feature_store)

redis_feature_store = json.dumps(j_feature_store)
redis_feature_store = redis_feature_store.replace('}, "', '},\n"')
# print(redis_feature_store)
f_res = open('./feature_store', 'w')
f_res.write(redis_feature_store)
f_res.close()
